﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NonBinaryTree
{
    public class NonBinaryTreeNode<T>
         where T : NonBinaryTreeNode<T>
    {
        private T parent;

        public NonBinaryTreeNode()
        {
            Parent = null;
            Children = new NonBinaryTreeNodeList<T>(this);
        }

        public NonBinaryTreeNode(T parentNode)
        {
            Parent = parentNode;
            Children = new NonBinaryTreeNodeList<T>(this);
        }

        public NonBinaryTreeNode(NonBinaryTreeNodeList<T> childNodes)
        {
            Parent = null;
            Children = childNodes;
            Children.Parent = (T)this;
        }

        public NonBinaryTreeNode(T parentNode, NonBinaryTreeNodeList<T> childNodes)
        {
            Parent = parentNode;
            Children = childNodes;
            Children.Parent = (T)this;
        }

        public T Parent
        {
            get
            {
                return parent;
            }

            set
            {
                if (this != null && this.Parent != null)
                {
                    parent.Children.Remove(this);
                    value.Children.Add(this);
                }

                parent = value;
            }
        }
        public virtual string Name { get; set; }
        public NonBinaryTreeNodeList<T> Children { get; set; }
    }
}
